package util;

import android.content.Context;
import android.widget.Toast;

import com.jiangman.weather.BaseApplication;
import com.jiangman.weather.bean.CityListResponse;
import com.jiangman.weather.bean.CommonResponse;
import com.jiangman.weather.bean.ConstantMsg;
import com.jiangman.weather.bean.LoginResponse;
import com.jiangman.weather.bean.MessageEvent;
import com.jiangman.weather.callback.CustomCallback;
import com.jiangman.weather.callback.RsponseStateListenerImp;
import com.jiangman.weather.constant.ConstantSp;
import com.jiangman.weather.constant.ConstantUrl;
import com.jiangman.weather.db.dao.WeatherDao;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by mk on 17-5-6.
 */

public class HttpRequestUtils {
    Context mContext;

    public HttpRequestUtils() {
    }

    public HttpRequestUtils(Context context) {
        this.mContext = context;

    }
    //// TODO: 17-5-6

    public void addCityToService(String areaId, String name, String mainarea) {
        System.out.println("地点："+name);
        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl + "Weather/insertCity")
                .addParams("u_id", ConstantSp.USER_ID)
                .addParams("areaId", areaId)
                .addParams("name",name)
                .addParams("mainarea", mainarea)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<CommonResponse>() {

                    @Override
                    public void fail(CommonResponse response) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void success(CommonResponse response, boolean isRefresh) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void error(Exception e) {
                        ToastUitl.show("亲，网络出错了", Toast.LENGTH_SHORT);
                    }
                }, CommonResponse.class, false));
    }

    public void updateCityToService(String areaId, String mainarea) {
//        System.out.println("地点："+);
        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl + "Weather/updateCityState")
                .addParams("uId", ConstantSp.USER_ID)
                .addParams("mainarea", mainarea)
                .addParams("areaId", areaId)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<CommonResponse>() {

                    @Override
                    public void fail(CommonResponse response) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void success(CommonResponse response, boolean isRefresh) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void error(Exception e) {
                        ToastUitl.show("亲，网络出错了", Toast.LENGTH_SHORT);
                    }
                }, CommonResponse.class, false));
    }


    public void deleteCity(String areaId) {
        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl + "Weather/deleteCity")
                .addParams("uId", ConstantSp.USER_ID)
                .addParams("areaId", areaId)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<CommonResponse>() {

                    @Override
                    public void fail(CommonResponse response) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void success(CommonResponse response, boolean isRefresh) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void error(Exception e) {
                        ToastUitl.show("亲，网络出错了", Toast.LENGTH_SHORT);
                    }
                }, CommonResponse.class, false));
    }

    public void selectAllCity() {
        System.out.println("fsdf:"+ConstantSp.USER_ID);

        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl + "Weather/selectUserCity")
                .addParams("uId", ConstantSp.USER_ID)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<CityListResponse>() {

                    @Override
                    public void fail(CityListResponse response) {
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void success(CityListResponse response, boolean isRefresh) {
                        List<CityListResponse.DataBean> data = response.data;
                        setData(data);
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void error(Exception e) {
                        ToastUitl.show("亲，网络出错了", Toast.LENGTH_SHORT);
                    }
                }, CityListResponse.class, false));
    }

    public void setData(List<CityListResponse.DataBean> data) {

        WeatherDao dao = new WeatherDao(mContext);
        for (CityListResponse.DataBean item : data) {
            dao.addToAreas(String.valueOf(item.area_weather_id ), item.name);
        }

    }




    public void setAutoUserLogin(String userName,String userPwd){

        OkHttpUtils.post()
                .url(ConstantUrl.baseUrl+"Weather/login")
                .addParams("user_name",userName)
                .addParams("user_pwd",userPwd)
                .build()
                .execute(new CustomCallback(new RsponseStateListenerImp<LoginResponse>() {

                    @Override
                    public void fail(LoginResponse response) {


                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);

                    }

                    @Override
                    public void success(LoginResponse response, boolean isRefresh) {
                        LoginResponse.DataBean data = response.data;
                        ConstantSp.USER_ID=String.valueOf(data.id);
                        ConstantSp.USER_NAME=data.username;
                        MessageEvent messageEvent = new MessageEvent();
                        messageEvent.user_name=ConstantSp.USER_NAME;
                        messageEvent.message_id= ConstantMsg.update_user;
                        EventBus.getDefault().post(messageEvent);
                        ToastUitl.show(response.msg, Toast.LENGTH_SHORT);
                        BaseApplication.isLogin=true;

                    }

                    @Override
                    public void error(Exception e) {
                        System.out.println(e.toString());
                    }
                }, LoginResponse.class, false));
    }
}
